package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.News;
import com.ly.pojo.dto.NewsDTO;
import com.ly.pojo.vo.NewsVO;
import com.ly.service.NewsService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName NewsController
 * @date : 2025/2/10 21:47
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/news")
@Slf4j
public class NewsController {
    @Autowired
    private NewsService newsService;

    @GetMapping("/list")
    public Result list(){
        List<News> list = newsService.list();
        return Result.success(list);
    }

    @GetMapping("/getNewsNum")
    @ExcludeToken
    public Result getNewsNum(){
        long count = newsService.count();
        return Result.success(count);
    }

    @GetMapping("/getNewsNumByTeamId/{teamId}")
    @ExcludeToken
    public Result getNewsNumByTeamId(@PathVariable Integer teamId){
        long count = newsService.getNewsNumByTeamId(teamId);
        return Result.success(count);
    }

    @GetMapping("/getNewsLast/{teamId}")
    @ExcludeToken
    public Result getNewsLast(@PathVariable Integer teamId){
        List<News> newsLast = newsService.getNewsLast(teamId);
        return Result.success(newsLast);
    }


    @GetMapping("/getNewsClickNum")
    @ExcludeToken
    public Result getNewsClickNum(){
        List<NewsVO> newsClickNum = newsService.getNewsClickNum();
        return Result.success(newsClickNum);
    }

    @GetMapping("/Allpage")
    public Object Allpage( NewsDTO newsDTO){
        log.info("{}",newsDTO);
        IPage<NewsVO> page=null;
        page= newsService.getAllPage(newsDTO);
        return Result.success(page);
    }

    @GetMapping("/page")
    public Object page( NewsDTO newsDTO){
        log.info("{}",newsDTO);
        IPage<NewsVO> page=null;
        page= newsService.getPage(newsDTO);
        return Result.success(page);
    }

    @PostMapping("/save")
    public Result save(@RequestBody News news){
        newsService.save(news);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody News news){
        newsService.updateById(news);
        return Result.success();
    }

    @PutMapping("/addClickNum")
    @ExcludeToken
    public Result addClickNum(@RequestBody News news){
        newsService.addClickNum(news);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        newsService.removeById(id);
        return Result.success("删除成功");
    }


}
